//package com.yunxin.core.domain.service.warn.impl;
//
//
//import cn.hutool.core.collection.CollUtil;
//import cn.hutool.core.collection.ListUtil;
//import cn.hutool.core.date.DatePattern;
//import cn.hutool.core.date.DateTime;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.yunxin.common.utils.JacksonUtils;
//import com.yunxin.core.application.dto.warn.WarnCheckChannelDTO;
//import com.yunxin.core.domain.external.product.ProductExternalService;
//import com.yunxin.core.domain.external.warn.WarnInfoChannelDTO;
//import com.yunxin.core.domain.external.warn.WarnInfoMerchantDTO;
//import com.yunxin.core.domain.model.warn.WarnHistory;
//import com.yunxin.core.domain.model.warn.WarnUser;
//import com.yunxin.core.domain.service.warn.IWarnHistoryService;
//import com.yunxin.core.domain.service.warn.IWarnUserService;
//import com.yunxin.core.domain.service.warn.IWarnUserTypeService;
//import com.yunxin.core.infra.persistence.entity.channel.query.result.ChannelInfoResult;
//import com.yunxin.core.infra.persistence.entity.merchant.MerchantInfoDO;
//import com.yunxin.core.infra.persistence.entity.merchant.MerchantUserAccountDO;
//import com.yunxin.core.infra.persistence.entity.product.query.result.ProductAllocationDetailResult;
//import com.yunxin.core.infra.persistence.mapper.channel.ChannelInfoExtendMapper;
//import com.yunxin.core.infra.persistence.mapper.merchant.MerchantInfoMapper;
//import com.yunxin.core.infra.persistence.mapper.merchant.MerchantUserAccountExtendMapper;
//import com.yunxin.core.infra.persistence.mapper.merchant.MerchantUserAccountMapper;
//import com.yunxin.core.infra.persistence.mapper.warn.WarnHistoryMapper;
////import com.yunxin.statistics.infra.persistence.mapper.ChannelPromotionTransformStatisticsExtendMapper;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Async;
//import org.springframework.stereotype.Service;
//
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//import java.util.function.Function;
//import java.util.stream.Collectors;
//
///**
// * <p>
// * 服务实现类
// * </p>
// *
// * @author author
// * @since 2024-05-13
// */
//@Service
//@Slf4j
//public class WarnHistoryServiceImpl extends ServiceImpl<WarnHistoryMapper, WarnHistory> implements IWarnHistoryService {
//
//
//
//    @Autowired
//    private ChannelInfoExtendMapper channelInfoExtendMapper;
//
//    @Autowired
//    private IWarnUserService warnUserService;
//
//    @Autowired
//    private SendMsgService sendMsgService;
//
//    @Autowired
//    private ProductExternalService productExternalService;
//
//
//    @Autowired
//    private MerchantInfoMapper merchantInfoMapper;
//
//    @Autowired
//    private MerchantUserAccountMapper merchantUserAccountMapper;
//
//
//    private static final String channelMsgTpl = """
//            【腰袋钱包】渠道发生漏损,请尽快处理告警信息！！！
//            """;
//    private static final String merchantMsgTpl = """
//            【腰袋钱包】商户接单状态关闭,请尽快处理告警信息！！！
//            """;
//
//    @Async
//    @Override
//    public void checkByMerchant(Long merchantId) {
//
//        List<Long> customerServiceIds = merchantUserAccountMapper.selectMerchantUidByRoles(merchantId, List.of(4L));
//
//        List<List<Long>> list = ListUtil.partition(customerServiceIds, 500);
//
//        int total = 0;
//        for (List<Long> ids : list) {
//            Integer num = merchantUserAccountMapper.countNotTakeOrders(merchantId,ids);
//            total += num;
//        }
//
//        //客服总人数 与 关闭接单人数不相等
//        if (customerServiceIds.size() != total) {
//            return;
//        }
//
//
//        MerchantInfoDO merchantInfoDO = merchantInfoMapper.selectByPrimaryKey(merchantId);
//
//
//
//        //查询是否有在线的产品
//        List<ProductAllocationDetailResult> productAllocationDetailResults = productExternalService.allCheckMerchant(merchantId);
//
//        if (CollUtil.isEmpty(productAllocationDetailResults)) {
//            return;
//        }
//
//        List<WarnUser> warnUsers = warnUserService.selectUserByType(1);
//        try {
//            List<String> phoneList = warnUsers.stream().map(WarnUser::getPhone).toList();
//            sendMsgService.send(phoneList, merchantMsgTpl, "商户告警");
//        } catch (Exception e) {
//            log.error("商户告警失败,商户={},msg={}", merchantInfoDO.getMerchantName(), merchantMsgTpl);
//        }
//
//        List<WarnHistory> historyList = new ArrayList<>();
//        Date sendTime = new Date();
//        String merchantIdStr = merchantId.toString();
//        for (ProductAllocationDetailResult productAllocationDetailResult : productAllocationDetailResults) {
//
//
//            //展示的信息
//            WarnInfoMerchantDTO infoMerchantDTO = new WarnInfoMerchantDTO();
//            infoMerchantDTO.setMerchantId(merchantId);
//            infoMerchantDTO.setMerchantName(merchantInfoDO.getMerchantName());
//            infoMerchantDTO.setProductName(productAllocationDetailResult.getProductName());
//            infoMerchantDTO.setProductId(productAllocationDetailResult.getProductId());
//            infoMerchantDTO.setCreateTime(DateUtil.format(sendTime, DatePattern.NORM_DATETIME_PATTERN));
//            String json = JacksonUtils.X.format(infoMerchantDTO);
//
//
//            WarnHistory warnHistory = new WarnHistory();
//            warnHistory.setMsg(merchantMsgTpl);
//            warnHistory.setCreateTime(sendTime);
//            warnHistory.setWarnType(1);
//            warnHistory.setMetaKey(merchantIdStr);
//            warnHistory.setMetaData(json);
//            historyList.add(warnHistory);
//        }
//        this.saveBatch(historyList);
//    }
//
//    @Override
//    public void checkChannel() {
//
//        DateTime date = DateUtil.date();
//
//        String startTime = DateUtil.format(DateUtil.beginOfDay(date), DatePattern.NORM_DATETIME_PATTERN);
//        String endTime = DateUtil.format(DateUtil.endOfDay(date), DatePattern.NORM_DATETIME_PATTERN);
//
//
//        List<WarnCheckChannelDTO> warnCheckChannelDTOS = this.baseMapper.checkChannel(startTime, endTime);
//
//
//        if (CollUtil.isEmpty(warnCheckChannelDTOS)) {
//            return;
//        }
//
//
//        List<WarnUser> warnUsers = warnUserService.selectUserByType(2);
//
//
//        List<ChannelInfoResult> results = channelInfoExtendMapper.selectByIds(warnCheckChannelDTOS.stream().map(WarnCheckChannelDTO::getChannelId).toList());
//
//        Map<Long, ChannelInfoResult> channelInfoResultMap = results.stream().collect(Collectors.toMap(ChannelInfoResult::getId, Function.identity()));
//
//
//        List<WarnHistory> historyList = new ArrayList<>();
//        for (WarnCheckChannelDTO dto : warnCheckChannelDTOS) {
//
//
//            Long formDeal = dto.getFormDeal();
//
//            Long systemFrom = dto.getSystemFrom();
//
//            if (!systemFrom.equals(formDeal)) {
//                Date sendTime = new Date();
//                String id = dto.getChannelId().toString();
//                String channelName = channelInfoResultMap.get(dto.getChannelId()).getChannelName();
//
//                //展示的信息
//                WarnInfoChannelDTO channelDTO = new WarnInfoChannelDTO();
//                channelDTO.setChannelName(channelName);
//                channelDTO.setNum(systemFrom - formDeal);
//                channelDTO.setCreateTime(DateUtil.format(sendTime, DatePattern.NORM_DATETIME_PATTERN));
//                String json = JacksonUtils.X.format(channelDTO);
//
//
//                WarnHistory warnHistory = new WarnHistory();
//                warnHistory.setMsg(channelMsgTpl);
//                warnHistory.setCreateTime(sendTime);
//                warnHistory.setWarnType(2);
//                warnHistory.setMetaKey(id);
//                warnHistory.setMetaData(json);
//                historyList.add(warnHistory);
//            }
//
//        }
//        try {
//            List<String> phoneList = warnUsers.stream().map(WarnUser::getPhone).toList();
//            sendMsgService.send(phoneList, channelMsgTpl, "渠道告警");
//        } catch (Exception e) {
//            log.error("渠道告警失败,msg={}", channelMsgTpl);
//        }
//        this.saveBatch(historyList);
//
//    }
//
//
//}
